# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#文件：post_batch.py
触发各个业务系统的自动任务
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import requests

from msydwork.config.config_msydloan import c_url, w_url

headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'
        }

headers1 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    'content-Type': "application/x-www-form-urlencoded",
    "Cookie": "admint=64b9d0c56cfb5649da4d98c82a20c40e8440e528; adminhostid=3"
}

qdzj_url = 'https://test.coo.msxiaodai.com/'
msydloan_url = 'https://test.zyjk.msxiaodai.com/'
mssa_ip = 'http://10.16.20.182:8003/'
bxloan_ip = 'http://10.16.20.182:9004/'


down_file = msydloan_url + 'file/loanFile/download'

qdzj_batch = qdzj_url + 'system/triggerBatchProcess'        # 渠道资金

msydloan_ip = c_url             # 自动任务ip
batch = msydloan_ip + '/system/loanProcessJob'              # 不同业务系统url不同
# 中移动独有
batch_loanFile = msydloan_ip + '/system/loanFile'
batch_refund = msydloan_ip +'/system/refundFile'
batch_settle = msydloan_ip +'/system/settleFile'
batch_repay = msydloan_ip +'/system/repayFile'
batch_repayResult = msydloan_ip +'/system/resultDetailFile'

# 开启和关闭mq
restart_mq = msydloan_url + 'mqManage/restartMessageListener'         # 重启mq监听
stop_mq = msydloan_url + 'mqManage/stopMessageListener'               # 停止mq监听

# 账务跑批
def exec_batch(url):
    batch = url + '/system/loanProcessJob'
    req1 = requests.post(batch, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

# application/x-www-form-urlencoded
def exec_loanFile():
    req1 = requests.post(batch_loanFile,headers=headers1)
    print("执行自动任务完成：", req1, req1.text)

def exec_fileLog(c_url,channel,date):
    date = {"date":date}
    if channel == 'msydloan':
        ful_url = c_url + '/system/insertFileCronjobLog'
    else:
        ful_url = c_url + '/test/insertFileLog'
    req1 = requests.post(ful_url,data=date, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def read_file(c_url,channel,date):
    date = {"date":date}
    if channel == 'msydonline':
        ful_url = c_url + '/test/readFile'
    else:
        ful_url = c_url + '/test/'
    req1 = requests.post(ful_url,data=date, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def exec_refund():
    req1 = requests.post(batch_refund, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def exec_settle():
    req1 = requests.post(batch_settle, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def exec_repay():
    req1 = requests.post(batch_repay,headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def exec_repayResult():
    req1 = requests.post(batch_repayResult,headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def exec_stopMq():
    req1 = requests.post(stop_mq, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def exec_restartMq():
    req1 = requests.post(restart_mq, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

# 清除redis缓存
def clear_cache():
    ful_url = w_url + '/spare/loadingSystemCache'
    req1 = requests.post(ful_url, headers=headers)
    print("执行自动任务完成：", req1, req1.text)

# application/x-www-form-urlencoded
def download_file():
    req1 = requests.post(down_file,headers=headers1)
    print("执行自动任务完成：", req1, req1.text)

def jianXin_mark():
    ful_url = msydloan_url + 'jianxin/mark/runbatch'
    req1 = requests.get(ful_url,headers=headers)
    print("执行自动任务完成：", req1, req1.text)

def jianXin_mark1():
    ful_url = msydloan_url + '/jianxin/screening/runbatch'
    req1 = requests.get(ful_url,headers=headers)
    print("执行自动任务完成：", req1, req1.text)


if __name__ == '__main__':
    i = 2
    if  i == 1:
        exec_batch()             # 账务处理、批量扣款明细文件生成(可能生成空文件、回盘也是空文件)
    elif i == 2:
        exec_fileLog()           # 定时查看和包文件推送过来：批量扣款明细回盘文件、扣款结果明细文件、分省提前清贷/退货文件、分省提前清贷/退货结算文件)
    elif i == 3:
        exec_loanFile()          # 01分省业务办理确认文件接收(手动触发)hb_loanfile_detail+hb_loanfile
    elif i == 4:
        exec_refund()            # 02分省提前清贷/退货文件hb_refund_detail+hb_loanfile
    elif i == 5:
        exec_settle()            # 04分省提前清贷/退货结算文件hb_settle_detail+hb_loanfile
    elif i == 6:
        exec_repay()             # 批量扣款明细回盘文件hb_result_detail
    elif i == 7:
        exec_repayResult()       # 03扣款结果明细文件
    elif i == 8:
        exec_stopMq()            # 停止mq监听
    elif i == 9:
        exec_restartMq()         # 重启mq监听
    elif i == 10:
        jianXin_mark()
    elif i == 11:
        jianXin_mark1()
    elif i == 12:
        clear_cache()
    else:
        pass